import http from '@/common/http';
import Loading from '@/components/loading';
import { Dialog } from 'mand-mobile';
const defaultMessageState = () => {
  return {
    messages: null,
    systemMessageList: null,
    allotMessageList: null,
    activityMessageList: null,
    hudongMessageList: null,
    unreadMessageInfo: null
  };
};
const state = defaultMessageState();

const getters = {
  unreadMessage(state) {
    return state.unreadMessageInfo;
  },
  getMessageList(state) {
    return state.messages;
  },
  systemMessageList(state) {
    return state.systemMessageList;
  },
  allotMessageList(state) {
    return state.allotMessageList;
  },
  hudongMessageList(state) {
    return state.hudongMessageList;
  },
  activityMessageList(state) {
    return state.activityMessageList;
  }
};
const actions = {
  getUnreadMessageInfo(context) {
    return new Promise((resolve, reject) => {
      Loading.show();
      http.post('/pakj/passenger/getUserMsgFlag').then(res => {
        Loading.hide();
        if (res.data.code === '000000') {
          context.commit('setUnreadMessageInfo', res.data.data);
          resolve();
        } else {
          Dialog.alert({
            title: '错误',
            content: res.data.msg,
            confirmText: '确定'
          });
          reject(new Error(res.data.code + ':' + res.data.msg));
        }
      });
    });
  },
  getMessageList(context) {
    return new Promise((resolve, reject) => {
      Loading.show();
      http.post('pakj/common/getCommonlyMsg').then(res => {
        Loading.hide();
        if (res.data.code === '000000') {
          context.commit('setMessageList', res.data.data);
          resolve();
        } else {
          Dialog.alert({
            title: '错误',
            content: res.data.msg,
            confirmText: '确定'
          });
          reject(new Error(res.data.code + ':' + res.data.msg));
        }
      });
    });
  },
  deleteMessageItem(context, type) {
    return new Promise((resolve, reject) => {
      Loading.show();
      http.post('pakj/common/delMsg', { type: type }).then(res => {
        Loading.hide();
        if (res.data.code === '000000') {
          console.log('消息删除成功');
          resolve();
        } else {
          Dialog.alert({
            title: '错误',
            content: res.data.msg,
            confirmText: '确定'
          });
          reject(new Error(res.data.code + ':' + res.data.msg));
        }
      });
    });
  },
  getSystemMessageList(context) {
    return new Promise((resolve, reject) => {
      Loading.show();
      http
        .post('pakj/common/getMsgByType', {
          page: '0',
          rows: '20',
          type: '1'
        })
        .then(res => {
          Loading.hide();
          if (res.data.code === '000000') {
            context.commit('setSystemMessageList', res.data.data);
            resolve();
          } else {
            Dialog.alert({
              title: '错误',
              content: res.data.msg,
              confirmText: '确定'
            });
            reject(new Error(res.data.code + ':' + res.data.msg));
          }
        });
    });
  },
  getAllotMessageList(context) {
    return new Promise((resolve, reject) => {
      Loading.show();
      http
        .post('pakj/common/getMsgByType', {
          page: '0',
          rows: '20',
          type: '2'
        })
        .then(res => {
          Loading.hide();
          if (res.data.code === '000000') {
            context.commit('setAllotMessageList', res.data.data);
            resolve();
          } else {
            Dialog.alert({
              title: '错误',
              content: res.data.msg,
              confirmText: '确定'
            });
            reject(new Error(res.data.code + ':' + res.data.msg));
          }
        });
    });
  },
  getActivityMessageList(context) {
    return new Promise((resolve, reject) => {
      Loading.show();
      http
        .post('pakj/common/getMsgByType', {
          page: '0',
          rows: '20',
          type: '3'
        })
        .then(res => {
          Loading.hide();
          if (res.data.code === '000000') {
            context.commit('setActivityMessageList', res.data.data);
            resolve();
          } else {
            Dialog.alert({
              title: '错误',
              content: res.data.msg,
              confirmText: '确定'
            });
            reject(new Error(res.data.code + ':' + res.data.msg));
          }
        });
    });
  },
  getHudongMessageList(context) {
    return new Promise((resolve, reject) => {
      Loading.show();
      http
        .post('pakj/common/getMsgByType', {
          page: '0',
          rows: '20',
          type: '4'
        })
        .then(res => {
          Loading.hide();
          if (res.data.code === '000000') {
            context.commit('setHudongMessageList', res.data.data);
            resolve();
          } else {
            Dialog.alert({
              title: '错误',
              content: res.data.msg,
              confirmText: '确定'
            });
            reject(new Error(res.data.code + ':' + res.data.msg));
          }
        });
    });
  }
};
const mutations = {
  setUnreadMessageInfo(state, data) {
    state.unreadMessageInfo = data;
  },
  setMessageList(state, data) {
    state.messages = data;
  },
  setSystemMessageList(state, data) {
    state.systemMessageList = data;
  },
  setAllotMessageList(state, data) {
    state.allotMessageList = data;
  },
  setActivityMessageList(state, data) {
    state.activityMessageList = data;
  },
  setHudongMessageList(state, data) {
    state.hudongMessageList = data;
  }
};
export default {
  state,
  getters,
  actions,
  mutations
};
